//
//  AppDelegate.m
//  BaoDian
//
//  Created by mac on 2024/7/16.
//

#import "AppDelegate.h"
#import "TJMainController.h"
#import "TJLoginController.h"
#import "TJNavigationController.h"
#import <DouyinOpenSDK/DouyinOpenSDKApplicationDelegate.h>

 
 
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self doInit];
    [self settingKeyboardManager];
    [self registeredNotification];
    
    [[DouyinOpenSDKApplicationDelegate sharedInstance] registerAppId: @"aw99d9sdjslcmvky"];
    
    [self loadVersionData];
    
    sleep(1.5);
    return YES;
}

- (void)loadVersionData {
    
    [TJTemplatePresent getVersionDataComplete:^(NSDictionary * _Nonnull data, NSError * _Nullable error) {
            
        NSString *message = data[@"appDesc"];
        int updateType = [data[@"updateType"] intValue];
        NSString *appVersion = data[@"appVersion"];
        NSString *currentVersion = getAppVersion();
        NSString *downloadUrl = data[@"appUrl"];
        
        if (isEmptyString(downloadUrl)) {
            return;
        }

        if (compareNumberValue(appVersion, currentVersion) == NSOrderedDescending) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发现新版本" message:message preferredStyle:UIAlertControllerStyleAlert];
        
            if (updateType == 2) {
                [alert addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}]];
            }
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadUrl] options:@{} completionHandler:nil];
            }]];
        
            [[TJTools currentViewController] presentViewController:alert animated:YES completion:nil];
        }
    }];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self loadVersionData];
}
 
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
}

- (void)doInit {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    [self.window makeKeyAndVisible];
    [self gotoMainController];
}

- (void)settingKeyboardManager {
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 20;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysHide;
    [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)gotoMainController {
    
    if (TJUserInfo.isLogin) {
        self.window.rootViewController =  [[TJMainController alloc] init];
        [TJMinePresent getUserInfoComplete:nil];
    } else {
        self.window.rootViewController = [[TJNavigationController alloc] initWithRootViewController:[[TJLoginController alloc] init]];
    }
 
}
 
- (void)registeredNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOut) name:tokenErrorNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOut) name:logoutNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucceed) name:loginSucceedNotification object:nil];
}

- (void)loginSucceed {
    self.window.rootViewController =  [[TJMainController alloc] init];
}

- (void)logOut {
    [[SDImageCache sharedImageCache] clearMemory];
    [TJUserInfo clear];
    self.window.rootViewController = [[TJNavigationController alloc] initWithRootViewController:[[TJLoginController alloc] init]];
}


- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]
        ) {
        return YES;
    }

    return NO;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) {
        return YES;
    }

    return NO;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if ([[DouyinOpenSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:nil annotation:nil]) {
        return YES;
    }
    return NO;
}

@end

